namespace DotNetNuke.Modules.Admin.ResourceInstaller
{
    using ICSharpCode.SharpZipLib.Zip;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.IO;

    public class PaInstaller : ResourceInstallerBase
    {
        private PaFolderCollection _dnnFolders;
        private PaInstallInfo _installInfo;
        private Stream _zipStream;

        public PaInstaller(Stream inputStream, string SitePath)
        {
            this._installInfo = new PaInstallInfo();
            this.InstallerInfo.SitePath = SitePath;
            this._zipStream = inputStream;
        }

        public PaInstaller(string Path, string SitePath)
        {
            this._installInfo = new PaInstallInfo();
            this.InstallerInfo.SitePath = SitePath;
            this._zipStream = new FileStream(Path, FileMode.Open, FileAccess.Read);
        }

        public bool Install()
        {
            this.InstallerInfo.Log.StartJob(base.INSTALL_Start);
            try
            {
                this.ReadZipStream();
                PaDnnLoaderFactory Factory = new PaDnnLoaderFactory(this.InstallerInfo);
                this._dnnFolders = Factory.GetDnnAdapter().ReadDnn();
                Factory.GetDnnInstaller().Install(this._dnnFolders);
            }
            catch (Exception exception1)
            {
                
                Exception ex = exception1;
                this.InstallerInfo.Log.Add(ex);
                bool Install = false;
                
                return Install;
                
            }
            this.InstallerInfo.Log.EndJob(base.INSTALL_Success);
            return true;
        }

        private void ReadZipStream()
        {
            this.InstallerInfo.Log.StartJob(base.FILES_Reading);
            ZipInputStream unzip = new ZipInputStream(this.ZipStream);
            for (ZipEntry entry = unzip.GetNextEntry(); entry != null; entry = unzip.GetNextEntry())
            {
                if (!entry.IsDirectory)
                {
                    this.InstallerInfo.Log.AddInfo(base.FILE_Loading + entry.Name);
                    PaFile file = new PaFile(unzip, entry);
                    this.InstallerInfo.FileTable[file.Name.ToLower()] = file;
                    if (file.Type == PaFileType.Dnn)
                    {
                        if (this.InstallerInfo.DnnFile != null)
                        {
                            this.InstallerInfo.Log.AddFailure(base.EXCEPTION_MultipleDnn + this.InstallerInfo.DnnFile.Name + " and " + file.Name);
                        }
                        else
                        {
                            this.InstallerInfo.DnnFile = file;
                        }
                    }
                    this.InstallerInfo.Log.AddInfo(string.Format(base.FILE_ReadSuccess, file.FullName));
                }
            }
            if (this.InstallerInfo.DnnFile == null)
            {
                this.InstallerInfo.Log.AddFailure(base.EXCEPTION_MissingDnn);
            }
            if (!this.InstallerInfo.Log.Valid)
            {
                throw new Exception(base.EXCEPTION_FileLoad);
            }
            this.InstallerInfo.Log.EndJob(base.FILES_ReadingEnd);
        }

        public PaInstallInfo InstallerInfo
        {
            get
            {
                return this._installInfo;
            }
        }

        public Stream ZipStream
        {
            get
            {
                return this._zipStream;
            }
        }
    }
}

